<?php

namespace addons\addondev\command;

use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use addons\addondev\library\OutputFacade;
use think\console\Command;

class Addoncrud extends Command
{

    protected function configure()
    {
        parent::configure();
        $this->setName('addon-crud')
            ->addOption('addon', 'A', Option::VALUE_REQUIRED, 'addon name', null)
            ->addOption('table', 't', Option::VALUE_REQUIRED, 'table name without prefix', null)
            ->addOption('controller', 'c', Option::VALUE_OPTIONAL, 'controller name', null)
            ->addOption('model', 'm', Option::VALUE_OPTIONAL, 'model name', null)
            ->addOption('fields', 'i', Option::VALUE_OPTIONAL, 'model visible fields', null)
            ->addOption('force', 'f', Option::VALUE_OPTIONAL, 'force override or force delete,without tips', null)
            ->addOption('local', 'l', Option::VALUE_OPTIONAL, 'local model', 1)
            ->addOption('import', 'a', Option::VALUE_OPTIONAL, 'enable import function', 0)
            ->addOption('tree', 'T', Option::VALUE_OPTIONAL, 'enable tree view', 0)
            ->addOption('tagcontroller', 'C', Option::VALUE_OPTIONAL, 'tag controller name', null)
            ->addOption('relation', 'r', Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'relation table name without prefix', null)
            ->addOption('relationmodel', 'e', Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'relation model name', null)
            ->addOption('relationforeignkey', 'k', Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'relation foreign key', null)
            ->addOption('relationprimarykey', 'p', Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'relation primary key', null)
            ->addOption('relationfields', 's', Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'relation table fields', null)
            ->addOption('relationmode', 'o', Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'relation table mode,hasone/belongsto/hasmany', null)
            ->addOption('relationcontroller', 'w', Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'relation table controller,only work at hasmany mode', null)
            ->addOption('delete', 'd', Option::VALUE_OPTIONAL, 'delete all files generated by CRUD', null)
            ->addOption('menu', 'u', Option::VALUE_OPTIONAL, 'create menu when CRUD completed', null)
            ->addOption('setcheckboxsuffix', null, Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'automatically generate checkbox component with suffix', null)
            ->addOption('enumradiosuffix', null, Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'automatically generate radio component with suffix', null)
            ->addOption('imagefield', null, Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'automatically generate image component with suffix', null)
            ->addOption('filefield', null, Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'automatically generate file component with suffix', null)
            ->addOption('intdatesuffix', null, Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'automatically generate date component with suffix', null)
            ->addOption('switchsuffix', null, Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'automatically generate switch component with suffix', null)
            ->addOption('citysuffix', null, Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'automatically generate citypicker component with suffix', null)
            ->addOption('jsonsuffix', null, Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'automatically generate fieldlist component with suffix', null)
            ->addOption('tagsuffix', null, Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'automatically generate tag component with suffix', null)
            ->addOption('editorsuffix', null, Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'automatically generate editor component with suffix', null)
            ->addOption('selectpagesuffix', null, Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'automatically generate selectpage component with suffix', null)
            ->addOption('selectpagessuffix', null, Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'automatically generate multiple selectpage component with suffix', null)
            ->addOption('ignorefields', null, Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'ignore fields', null)
            ->addOption('sortfield', null, Option::VALUE_OPTIONAL, 'sort field', null)
            ->addOption('headingfilterfield', null, Option::VALUE_OPTIONAL, 'heading filter field', null)
            ->addOption('fixedcolumns', null, Option::VALUE_OPTIONAL, 'fixed columns', null)
            ->addOption('editorclass', null, Option::VALUE_OPTIONAL, 'automatically generate editor class', null)
            ->addOption('db', null, Option::VALUE_OPTIONAL, 'database config name', 'database')
            ->setDescription('Build Addon CRUD controller and model from table');
    }

    protected function execute(Input $input, Output $output)
    {
        $crud = new \addons\addondev\library\Crud();
        // 插件名称
        $crud->addon = $input->getOption('addon') ?: '';
        // 数据库
        $crud->db = $input->getOption('db');
        // 表名
        $crud->mtable = $input->getOption('table') ?: '';
        // 自定义控制器
        $crud->controller = $input->getOption('controller');
        // 自定义模型
        $crud->model = $input->getOption('model');
        // 自定义显示字段
        $crud->fields = $input->getOption('fields');
        // 强制覆盖
        $crud->force = $input->getOption('force');
        // 是否为本地model,为0时表示为全局model将会把model放在app/common/model中
        $crud->local_switch = $input->getOption('local');
        // 是否启用导入功能
        $crud->import_switch = $input->getOption('import');
        // 是否生成菜单
        $crud->menu_switch = $input->getOption("menu");
        // 是否删除模式
        $crud->delete_switch = $input->getOption("delete");
        // 是否树视图
        $crud->tree_switch = $input->getOption("tree");
        // 自定义控制器
        $crud->tagcontroller = $input->getOption('tagcontroller');
        // 关联表
        $crud->relation = $input->getOption('relation');
        // 自定义关联表模型
        $crud->relationmodels = $input->getOption('relationmodel');
        // 模式
        $crud->relationmode = $input->getOption('relationmode');
        // 外键
        $crud->relationforeignkey = $input->getOption('relationforeignkey');
        // 主键
        $crud->relationprimarykey = $input->getOption('relationprimarykey');
        // 关联表显示字段
        $crud->relationfields = $input->getOption('relationfields');
        // 关联表显示字段
        $crud->relationcontroller = $input->getOption('relationcontroller');
        // 复选框后缀
        $crud->setcheckboxsuffix = $input->getOption('setcheckboxsuffix');
        // 单选框后缀
        $crud->enumradiosuffix = $input->getOption('enumradiosuffix');
        // 图片后缀
        $crud->imagefield = $input->getOption('imagefield');
        // 文件后缀
        $crud->filefield = $input->getOption('filefield');
        // 标签后缀
        $crud->tagsuffix = $input->getOption('tagsuffix');
        // 日期后缀
        $crud->intdatesuffix = $input->getOption('intdatesuffix');
        // 开关后缀
        $crud->switchsuffix = $input->getOption('switchsuffix');
        // 富文本编辑器
        $crud->editorsuffix = $input->getOption('editorsuffix');
        // 城市后缀
        $crud->citysuffix = $input->getOption('citysuffix');
        // JSON配置后缀
        $crud->jsonsuffix = $input->getOption('jsonsuffix');
        // selectpage后缀
        $crud->selectpagesuffix = $input->getOption('selectpagesuffix');
        // selectpage多选后缀
        $crud->selectpagessuffix = $input->getOption('selectpagessuffix');
        // 排除字段
        $crud->ignoreFields = $input->getOption('ignorefields');
        // 排序字段
        $crud->sortfield = $input->getOption('sortfield');
        // 顶部筛选过滤字段
        $crud->headingfilterfield = $input->getOption('headingfilterfield');
        // 固定列数量
        $crud->fixedcolumns = $input->getOption('fixedcolumns');
        // 编辑器Class
        $crud->editorclass = $input->getOption('editorclass');
        // 继续生成菜单
        $outputFacade = new OutputFacade($output);
        $crud->genMenu($outputFacade, $crud->execute($outputFacade));
    }
}
